package display.screens {
	
	import display.screens.business.BusinessScreenContent;
	import display.scrollableWindow.ScrollableWindow;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import gs.TweenLite;
	
	import org.ilovecode.buttons.FloorButton;
	import org.ilovecode.display.FloorImage;
	import org.ilovecode.mvc.View;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * Screen displays business related content to user.
	 * 
	 * @author Vjekoslav Ratkajec
	 */
	public class BusinessScreen extends FloorScreen {
		
		//{ region Stage elements
		
		//Title which is displayed when user navigates from main navigation
		protected var _title:MovieClip;
		//Bounding box for floor graphics
		protected var _bound:MovieClip;
		
		//Business floor 1 graphic
		protected var _floor1:FloorImage;
		//Business floor 0 graphic
		protected var _floor0:FloorImage;
		//Business floor -1 graphic
		protected var _floorMinus1:FloorImage;
		
		//Business floor 1 button switch
		protected var _floor1Button:FloorButton;
		//Business floor 0 button switch
		protected var _floor0Button:FloorButton;
		//Business floor -1 button switch
		protected var _floorMinus1Button:FloorButton;
		
		//Download PDF button
		protected var _downloadPDFButton:SimpleButton;
		
		
		//Content which will be loaded in scrollable window
		private var content:BusinessScreenContent;
		//Window to display content
		public var contentWindow:ScrollableWindow;
		
		//} endregion
		
		//{ region Variables
		
		//Currently selected floor number (range is from -1 to 1, so -2 is set as default)
		private var _floorNumber:int = -2;
		//Animation duration
		private var duration:Number = 0.4;
		//Currently displayed garage
		private var currentFloor:FloorImage = null;
		
		//} endregion
		
		/**
		 * Create new instance of Business screen.
		 */
		public function BusinessScreen() {
			super(ScreenName.BUSINESS, "businessScreen");
		}
		
		override protected function construct():void
		{
			super.construct();
			
			_title = MovieClip(this.getChildByName("title"));
			_bound = MovieClip(this.getChildByName("bound"));
			
			var xml:XML = controller.getScreenXML("business");
			_floor1 = FloorImage(this.getChildByName("floor1"));
			_floor1.loadImage(XML(xml.floorImages.image.(@id == "1")).@url);
			_floor0 = FloorImage(this.getChildByName("floor0"));
			_floor0.loadImage(XML(xml.floorImages.image.(@id == "0")).@url);
			_floorMinus1 = FloorImage(this.getChildByName("floorMinus1"));
			_floorMinus1.loadImage(XML(xml.floorImages.image.(@id == "-1")).@url);
			
			/*_floor1Button = FloorButton(this.getChildByName("floor1ButtonRef"));
			_floor1Button.id = 1;
			_floorButtons.push(_floor1Button);
			_floor0Button = FloorButton(this.getChildByName("floor0ButtonRef"));
			_floor0Button.id = 0;
			_floorButtons.push(_floor0Button);
			_floorMinus1Button = FloorButton(this.getChildByName("floorMinus1ButtonRef"));
			_floorMinus1Button.id = -1;
			_floorButtons.push(_floorMinus1Button);*/
			
			_downloadPDFButton = SimpleButton(this.getChildByName("downloadPDFButton"));
			
			content = new BusinessScreenContent();
		}
		
		override protected function initText():void
		{
			// TODO Load text in business
			super.initText();
		}
		
		override protected function initUI():void
		{
			super.initUI();
			_floor1.alpha = 0;
			_floor0.alpha = 0;
			_floorMinus1.alpha = 0;
			_bound.visible = false;
			_downloadPDFButton.visible = false;
			
			initTextFloorButton(1);
			initTextFloorButton(0);
			initTextFloorButton(-1);
			
			contentWindow.configUI(900, 185);
			contentWindow.setContent(content);
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			_downloadPDFButton.addEventListener(MouseEvent.CLICK, downloadPDFButton_Click);
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			_downloadPDFButton.removeEventListener(MouseEvent.CLICK, downloadPDFButton_Click);
		}
		
		//{ region Handlers
		
		private function downloadPDFButton_Click(event:MouseEvent):void {
			var sign:String = (_floorNumber > 0) ? "" : "-";
			var url:String = "http://www.ban-centar.hr/content/pdf/garaza/PP" + sign + Math.abs(_floorNumber) + ".pdf";
			navigateToURL(new URLRequest(url), "_blank");
		}
		
		//} endregion
		
		//{ region Public
		
		/**
		 * Switch from current to desired floor.
		 * 
		 * @param	floorNumber	Floor number to show.
		 */
		override public function showFloor(floorNumber:int):void {
			var itemName:String = ((floorNumber < 0) ? "floorMinus" : "floor") + Math.abs(floorNumber);
			if (_floorNumber != floorNumber) {
				TweenLite.to(FloorImage(this[itemName]), duration, { alpha:1 } );
				
				if (currentFloor != null) {
					TweenLite.to(currentFloor, duration, { alpha:0 } );
				}
				currentFloor = FloorImage(this[itemName]);
			}
			_floorNumber = floorNumber;
			
			_title.visible = false;
			_bound.visible = true;
			_downloadPDFButton.visible = true;
		}
		
		//} endregion
		
		
	}

}